import requests
from lxml import etree
import csv
# url='https://movie.douban.com/top250'
#
# head1={
# 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
# }
#
# data1=requests.get(url,headers=head1)
#
# print(data1.text)
# with open('dbmovie.html','w',encoding='utf-8') as f:
#     f.write(data1.text)

with open('dbmovie.html','r',encoding='utf-8') as f1:
    html1=f1.read()
html_work=etree.HTML(html1)
html_movieName=html_work.xpath('//*[@id="content"]/div/div[1]/ol/li/div/div[2]/div[1]/a/span[1]/text()')
html_movieUrl=html_work.xpath('//*[@id="content"]/div/div[1]/ol/li/div/div[2]/div[1]/a/@href')
html_moviePl=html_work.xpath('//*[@id="content"]/div/div[1]/ol/li/div/div[2]/div[2]/p[2]/span/text()')

list_movie=[]
for j in range(25):
    dic={}
    dic['电影名称']=html_movieName[j]
    dic['电影评论']=html_moviePl[j]
    dic['电影图片']=html_movieUrl[j]
    list_movie.append(dic)
print(list_movie)

data2=list_movie

head2=('电影名称','电影评论','电影图片')

with open('dbmovie.csv','w',encoding='utf-8-sig',newline='') as f2:
    ws = csv.DictWriter(f2,fieldnames=head2)
    ws.writeheader()
    ws.writerows(data2)